#include <iostream>
using namespace std;

struct TreeNode {
    int val;
    TreeNode* left;
    TreeNode* right;
    TreeNode(int x)
	: val(x)
	, left(NULL)
	, right(NULL)
    {
    }
};

int proc(TreeNode* root,int &res)
{
    if (root == nullptr) {
	return 0;
    }
    int left = proc(root->left,res);
    int right = proc(root->right,res);
    int leftt=((root->left != nullptr) && (root->left->val == root->val))?(left+1):(0);
    int rightt=((root->right != nullptr) && (root->right->val == root->val))?(right+1):(0);
    res=(res > (leftt+rightt))?(res):(leftt+rightt);
    return (leftt > rightt)?(leftt):(rightt);
}

int longestUnivaluePath(TreeNode* root)
{
    if (root == nullptr) {
	return 0;
    }
    int res = 0;
    proc(root, res);
    return res;
}

int main()
{
}
